//
//  YXNewfeaturePageView.m
//  Newfeature
//
//  Created by 周丽丽 on 16/8/22.
//  Copyright © 2016年 周丽丽. All rights reserved.
//

#import "YXNewfeaturePageView.h"
#import "Masonry.h"
#import "YXNewcharcateristicInterfaceModel.h"
@interface YXNewfeaturePageView ()<UITextViewDelegate>
@property (nonatomic, strong) UIImageView *newfeatureImageView;
@property (nonatomic, strong) UIButton *experienceButton;
@property (nonatomic,strong) UITextView *textView;
@property (nonatomic,strong) MASConstraint *TextViewHeightConstraint;
@end
@implementation YXNewfeaturePageView
- (void)setupPageViewWithIndex:(NSInteger)index{
    UIImageView *newfeatureImageView = [[UIImageView alloc]init];
    newfeatureImageView.image = [UIImage imageNamed:self.textDataModel.guidePictureName];
    [self addSubview:newfeatureImageView];
    self.newfeatureImageView = newfeatureImageView;
    
    UITextView *textV = [[UITextView alloc]init];
    [self addSubview:textV];
    self.textView = textV;
    [self setupAttributedText];
    self.textView.delegate = self;
    self.textView.textAlignment = NSTextAlignmentCenter;
    if (index == YXNewfeatureImageCount - 1) {
        [self setupExperienceButton:self];
    }
}
- (void)setupExperienceButton:(YXNewfeaturePageView *)pageView
{
    UIButton *expButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    expButton.layer.cornerRadius = 17;
    expButton.layer.borderColor = [UIColor colorWithHexString:@"ff7000"].CGColor;
    expButton.layer.borderWidth = 1;
    expButton.layer.masksToBounds = YES;
    self.userInteractionEnabled = YES;
    [self addSubview:expButton];
    self.experienceButton = expButton;
    [expButton setTitleColor:[UIColor colorWithHexString:@"ff7000"] forState:UIControlStateNormal];
    [expButton setTitle:@"进入泡泡" forState:UIControlStateNormal];
    [expButton setTitle:@"" forState:UIControlStateHighlighted];
    expButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [expButton addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];
    [self.experienceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textView.mas_bottom).offset(15 * AutoSizeScaleX);
        make.centerX.equalTo(self);
        make.width.mas_equalTo(140 * AutoSizeScaleX);
        make.height.mas_equalTo(34 * AutoSizeScaleX);
    }];}
- (void)start:(UIButton *)startButton{
    if (self.delegate) {
        [self.delegate startButtonClick:startButton];
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self.newfeatureImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(61 * AutoSizeScaleX);
        make.centerX.equalTo(self);
        CGFloat width = MIN(339*AutoSizeScaleX, [UIScreen mainScreen].bounds.size.height/2);
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(width);
    }];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.newfeatureImageView.mas_bottom).offset(15 * AutoSizeScaleX);
        make.centerX.equalTo(self.newfeatureImageView);
        make.width.equalTo(self.newfeatureImageView);
    }];
}
- (void)setupAttributedText{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.firstLineHeadIndent = 2;
    paraStyle.lineSpacing = 15 * AutoSizeScaleX;
    NSDictionary *attrDict = @{ NSParagraphStyleAttributeName: paraStyle,
                                  NSFontAttributeName: [UIFont systemFontOfSize: 20] };
    NSString *textViewStr = [self.textDataModel.textStr stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    [self.TextViewHeightConstraint uninstall];
    [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(2 * [self heightForString:textViewStr andWidth:self.width] * AutoSizeScaleX);
    }];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:textViewStr attributes:attrDict];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:self.textDataModel.specialTextColor] range:NSMakeRange(0,4)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:self.textDataModel.generalTextColor] range:NSMakeRange(4,14)];
    self.textView.attributedText = attributedString;
    
}
- (float) heightForString:(NSString *)str andWidth:(float)width{
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:str];
    self.textView.attributedText = attrStr;
    NSRange range = NSMakeRange(0, attrStr.length);
    NSDictionary *dic = [attrStr attributesAtIndex:0 effectiveRange:&range];
    CGSize sizeToFit = [str boundingRectWithSize:CGSizeMake(width - 16.0, MAXFLOAT)                                           options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading                                         attributes:dic  context:nil].size;
    return sizeToFit.height + 16.0;
}

@end
